package com.lemon.funplus;

import static android.view.Gravity.START;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Base64;
import android.util.Log;
import android.widget.FrameLayout;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.funplus.app.AppController;
import com.lemon.funplus.fragment.MenuFragment;
import com.lemon.funplus.view.CustomActionbar;

public class BaseActivity extends FragmentActivity {

	public CustomActionbar myActionBar;
	public DrawerLayout drawer;
	public MenuFragment menuFragment;
	FrameLayout frame_content;
	public ImageLoader imageLoader = AppController.getInstance().getImageLoader();
	int onStartCount = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getKeyHashes(getApplicationContext());
		onStartCount = 1;
		if (savedInstanceState == null)
			this.overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
		else
			onStartCount = 2;
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (onStartCount > 1) {
			this.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
		} else if (onStartCount == 1) {
			onStartCount++;
		}

	}

	@Override
	public void setContentView(int layoutResID) {
		drawer = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
		frame_content = (FrameLayout) drawer.findViewById(R.id.frame_main);
		setContentView(drawer);
		getLayoutInflater().inflate(layoutResID, frame_content, true);
		drawer.closeDrawer(START);
		initMenu();
	}

	void initMenu() {
		myActionBar = (CustomActionbar) findViewById(R.id.main_actionbar);
		myActionBar.setDrawer(drawer);
		menuFragment = new MenuFragment();
		menuFragment.setDrawerLayout(drawer);
		FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		fragmentTransaction.replace(R.id.frame_menu, menuFragment, menuFragment.getClass().getName());
		fragmentTransaction.commit();
	}
	
	@Override
	protected void onResume() {
		try {
			initMenu();
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onResume();
	}
	
	@TargetApi(Build.VERSION_CODES.FROYO)
	public static final void getKeyHashes(Context context) {
		PackageInfo packageInfo;
		try {
			// packageInfo =
			// getPackageManager().getPackageInfo("casino.vn",PackageManager.GET_SIGNATURES);
			packageInfo = context.getPackageManager().getPackageInfo(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName,
					PackageManager.GET_SIGNATURES);
			for (Signature signature : packageInfo.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				String key = new String(Base64.encode(md.digest(), 0));
				// String key = new String(Base64.encodeBytes(md.digest()));
				Log.e("Hash key", key);
			}
		} catch (NameNotFoundException e1) {
			Log.e("Name not found", e1.toString());
		} catch (NoSuchAlgorithmException e) {
			Log.e("No such an algorithm", e.toString());
		} catch (Exception e) {
			Log.e("Exception", e.toString());
		}
	}
}
